from product_upload.util.basic.mysql_util import db_get_one

class BasicDoba:
    # 字段顺序与数据库字段一致
    fields = [
        'id', 'title', 'spu', 'sku', 'region', 'supplier_code', 'supplier_name', 'brand',
        'link', 'quantity', 'price', 'main_image', 'dimensions', 'packages', 'json_text',
        'product_info', 'create_time', 'update_time', 'remark', 'update_by',"not_available","class_name",
    ]

    def __init__(self, **kwargs):
        self.id = kwargs.get('id', None)
        self.title = kwargs.get('title', "")
        self.spu = kwargs.get('spu', "")
        self.sku = kwargs.get('sku', "")
        self.region = kwargs.get('region', "")
        self.supplier_code = kwargs.get('supplier_code', "")
        self.supplier_name = kwargs.get('supplier_name', "")
        self.brand = kwargs.get('brand', "")
        self.link = kwargs.get('link', "")
        self.quantity = kwargs.get('quantity', 0)
        self.price = kwargs.get('price', None)
        self.main_image = kwargs.get('main_image', "")
        self.dimensions = kwargs.get('dimensions', "")
        self.packages = kwargs.get('packages', "")
        self.json_text = kwargs.get('json_text', "")
        self.product_info = kwargs.get('product_info', "")
        self.create_time = kwargs.get('create_time', None)
        self.update_time = kwargs.get('update_time', None)
        self.remark = kwargs.get('remark', "")
        self.update_by = kwargs.get('update_by', "")
        self.not_available = kwargs.get('not_available', "")
        self.class_name = kwargs.get('class_name', "")

    @classmethod
    def from_db(cls, field_names, row) -> 'BasicDoba':
        if len(field_names) != len(row):
            raise ValueError("bug!---db field len != query field len")
        data_dict = dict(zip(field_names, row))
        return cls(**data_dict)

    def __repr__(self):
        field_info = []
        for field in self.fields:
            value = getattr(self, field)
            field_info.append(f"{field}={repr(value)}")
        return f"BasicDoba=[{', '.join(field_info)}]"


if __name__ == '__main__':
    print(db_get_one("basic_doba", "id=1", BasicDoba))
